package custom.cards;


import custom.cards.GameRequest.RequestType;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gcm.GCMRegistrar;
import android.content.Context;
import android.content.Intent;

public class MainActivity extends Activity
{
	public static ResponseListener rl = new ResponseListener();
	

	public static Activity parent;
	public static String playerName;
	public static final String SENDER_ID = "1015096437595";
	public static Handler mHandler;
	/*
	 * "Enum" of ResponseTypes the server can send. NOT using real enum because than we have to use .toString() for every call of it
	 *  Also these are retrieved via getStringExtra as they are strings
	 */
	public static final String[] ResponseTypes = { 
		"PassCard",   // a player passed a card Extras: String Player (ID or DECK), String Card, String toPlayer (ID or DECK)
	    "GameCreated",// a confirmation that you either joined or created a new game Extras: String GameID of new game
	    "OK",		  // a generic message that may just want to display some message to the user Extras: String Message
	    "ERROR",	  // an error of some sort occurred while processing the game request Extras: String Message	
	    "GameWinner", // a player has won the game and this player has won Extras: String Player (ID)
	    "GameLoser",  // this player has lost the game based on being declared so Extras: String Player (ID)
	    "NewPlayer",  // sent at the start to notify the client of all the active players Extras: String Player (ID) String Name (DisplayName)
	    "PlayerLeft", // a player has left the game Extras: String Player (ID)
	    "CardsList"   // a list of cards of the requested player Extras: String Player (ID) String Cards (format: CardString;CardString;etc)
	    
	};
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		MainActivity.parent = this;
		if(playerName != null)
		{
			
			EditText name = (EditText)findViewById(R.id.playerNameET);
			
			name.setText(playerName);
			
		}
		
		
		 mHandler = new Handler(Looper.getMainLooper()) 
		 {
		 
			  /*
		         * handleMessage() defines the operations to perform when
		         * the Handler receives a new Message to process.
		         */
		        @Override
		        public void handleMessage(Message inputMessage) 
		        {
		            Intent i = (Intent) inputMessage.obj;
		            Log.d("handleMessage Main", "Fired: " + i.toString());
		           if(i != null)
		           {
		        	   try
		        	   {
		        		   ProcessIntent(i); 
		        	   }
		        	   catch(Exception ex)
		        	   {

		        		   Log.e("Exception in Main:", "Exception", ex);
		        	   } 
		           }
		           
		        }
			 
		 };
		


		
	
		
		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);
		final String regId = GCMRegistrar.getRegistrationId(this);
		
		if (regId.equals("")) 
		{
		  GCMRegistrar.register(this, SENDER_ID);
		} 
		else 
		{
		  // we have a regid already
			GamePlayActivity.playerID = regId;
		  GameRequest req = new GameRequest(regId);
		  new NetworkTask(req).execute();
		}
	
		
	}
	
	
	public void ProcessIntent(Intent i)
	{
		String type = i.getStringExtra("ResponseType");
		Log.d("ProcessIntent type:", type);
		if (type.equals("GameCreated"))
		 {

			  String r = i.getStringExtra("GameID");
			  
			  GamePlayActivity.gameID = r;
			 
			  if(GamePlayActivity.parent != null)
			  {
				  GamePlayActivity.parent.setTitle("In Game: " + r);
			  }
			  
			  GameRequest joinGame = new GameRequest(GamePlayActivity.gameID,GamePlayActivity.playerID,GamePlayActivity.playerID,RequestType.GetCards);
				 joinGame.toPlayerID = GamePlayActivity.playerID;
				  
				 new NetworkTask(joinGame).execute();
			  
			 
			  
		 }
		 else if(type.equals("OK") || type.equals("ERROR"))
		 {
			 String r = i.getStringExtra("Message");
			 
			 Toast.makeText(getApplicationContext(), r, Toast.LENGTH_LONG).show();
		 }
		 else
		 {
			// Toast.makeText(getApplicationContext(), "Unknown type notification: " + type, Toast.LENGTH_LONG).show();
		 }
	}
	
	
	public void joinClick(View v)
	{

		EditText name = (EditText)findViewById(R.id.playerNameET);
		
		String theName = name.getText().toString();
		
		if(theName != null)
		{
			if(!theName.isEmpty())
			{
				
				MainActivity.playerName = theName;
				EditText text = (EditText)findViewById(R.id.GameIDE);
				String theGame = text.getText().toString();
				
				if(theGame != null)
				{
					if(!theGame.isEmpty())
					{
						GamePlayActivity.gameToJoin = theGame;
						
						MainActivity.parent.startActivity(new Intent(MainActivity.parent, GamePlayActivity.class));
						return;
					}
					
				}
				
				Toast.makeText(this, "Please enter a valid GameID!", Toast.LENGTH_LONG).show();
				return;
				
			}
			
			
		}
		
		Toast.makeText(this, "Please enter a Player Name!", Toast.LENGTH_LONG).show();
		
			
		
		
	}
	

	
	public void btnClick(View v)
	{
		
		EditText name = (EditText)findViewById(R.id.playerNameET);
		
		if(name != null)
		{
			
		
			String theName = name.getText().toString();
			
			
			if(theName != null)
			{
				if(!theName.isEmpty())
				{
					GamePlayActivity.gameToJoin = null;
					MainActivity.playerName = theName;
					GameRequest newGame = new GameRequest(null,GamePlayActivity.playerID,playerName,"CardGame",RequestType.CreateGame);
					MainActivity.parent.startActivity(new Intent(MainActivity.parent, GamePlayActivity.class));
					new NetworkTask(newGame).execute();
					return;
				}
				
			}
			
			Toast.makeText(this, "Please enter a Player Name!", Toast.LENGTH_LONG).show();
		}
		else
		{
			
		}
		 
	}
	


	

}

